<?php
	include_once("../backend/dataprovider.php");
	session_start();
	if( !isset($_SESSION["loggedUser"]) || $_SESSION["loggedUser"]==null){
		header("Location:../index.php");
		return;
	}
	if(isset($_GET["txtUsername"]))
		$sql = "select a.id, u.username, title_vn, title_en, a.createdDate, a.active 
			from articles as a inner join users as u on a.userId = u.id
			where a.title_vn like '%" . $_GET["txtUsername"] . "%' order by a.createdDate desc";
	else
		$sql = "select a.id, u.username, title_vn, title_en, a.createdDate, a.active 
			from articles as a inner join users as u on a.userId = u.id order by a.createdDate desc";
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="admin.css"/>
		<!--<script type='text/javascript' src='../jquery-min.js'></script>-->
		
		<link rel="stylesheet" href="../jquery-ui-1.10.4/themes/base/jquery.ui.all.css">
		<script src="../jquery-ui-1.10.4/jquery-1.10.2.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.core.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.widget.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.mouse.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.button.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.draggable.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.position.js"></script>
		<script src="../jquery-ui-1.10.4/ui/jquery.ui.dialog.js"></script>
		
		<script type="text/javascript">
			$(document).ready(function(){
				$("#btnSearch").click(function(){
					$("#frmSearchUser").submit();
				});
				
				$("#btnReg").click(function(){
					window.location.replace("reg.php");
					/*
						OR we can use
						window.location.href = "";
					*/
				});
				
				$("#btnLogout").click(function(){
					$.ajax({
						url:"backend/user.php",
						cache:false,
						type:'POST',
						data:{ACTION:'LOGOUT'},
						success:function(responseText){
							//alert(responseText);
							
							var rs = $.parseJSON(responseText);
							$('#div_user_pane').fadeOut();
							$('#div_login').fadeIn();
							
						}
					});
					return false;
				});
				
				$("#div_main").on("click", "a.clsdelpost", function(){
					var articleId = $(this).attr("id");
					$( "#dialog-confirm" ).dialog({
						resizable: false,
						height:140,
						modal: true,
						buttons: {
							"Delete": function() {
								$( this ).dialog( "close" );
								
								$.ajax({
									url:"backend/post.php",
									cache:false,
									type:"POST",
									data:{ACTION:"DELETEARTICLE", id:articleId},
									success:function(responseText){
										//alert(responseText);
										var rs = $.parseJSON(responseText);
										if(rs.status == "OK"){
											$("#article"+articleId).remove();
										}
									}
								});
								
							},
							Cancel: function() {
								$( this ).dialog( "close" );
							}
						}
					});
					/*
					var articleId = $(this).attr("id");
					$.ajax({
						url:"backend/post.php",
						cache:false,
						type:"POST",
						data:{ACTION:"DELETEARTICLE", id:articleId},
						success:function(responseText){
							alert(responseText);
							var rs = $.parseJSON(responseText);
							if(rs.status == "OK"){
								$("#article"+articleId).remove();
							}
						}
					});
					*/
					return false;
				});
				
				$("#div_main").on("change", "input.clschkactive", function(){
					var articleId = ($(this).attr("id")).replace("chk","");
					var active = $(this).is(":checked");
					//alert("Article: " + articleId + ", active: " + active);
					$.ajax({
						url:"backend/post.php",
						cache:false,
						type:"POST",
						data:{ACTION:"SETACTIVE",id:articleId,isActive:active},
						success:function(responseText){
							/*
							alert(responseText);
							var rs = $.parseJSON(responseText);
							if(rs.status == "OK"){
								$("#article"+articleId).remove();
							}
							*/
						}
					});
					return false;
				});
			});
			
		</script>
		
		<title>VTech blog - A technical blog</title>
	</head>
	<body>
		<div id="container">
			<div id="div_header">
				<div id="div_user_pane">
					<span style="padding-left:30px;color:#dafafa;">Xin chao </span>
					<a href="#" style="text-decoration:none;color:#ffff22;" ><span style="font-weight:bold;"><?php echo $_SESSION["loggedUser"]; ?></span></a>
					<a href="#" style="text-decoration:none;color:#ffdada" id="btnLogout">Logout</a>
				</div>
			</div>
			<div id="div_nav" class="menu">
				<ul>
					<li><a href="../index.php">Home</a></li>
					<li><a href="../funct.php">NMath</a></li>
					<?php
						if(isset($_SESSION["loggedUser"])){
							if($_SESSION["loggedUser"]=='davu' || $_SESSION["loggedUser"]=='admin'|| $_SESSION["loggedUser"]=='huong'){
								print "<li><a href='dashboard.php'>Administrative</a></li>";
							}
						}
					?>
					<li><a href="../about.php">About me</a></li>
				</ul>
			</div>
			<div id="sidebar">
				<ul style="list-style-type:none">
					<li><a href="dashboard.php">Dashboard</a></li>
					<li><a href="userlst.php">Users</a></li>
					<li>Posts</li>
					<li><a href="files.php">Files</a></li>
					<li><a href="../about.php">Application</a></li>
				</ul>
			</div>
			<div id="div_main">
				<div>
					<form id="frmSearchUser" name="frmSearchUser" action="articles.php">
						<input type="text" name="txtUsername" class="searchText" />
						<input type="submit" name="btnSearch" value="Search"/>
					</form>
					<a href="newpost.php">New post</a>
				</div>
				<div id="dialog-confirm" style="display:none;" title="Empty the recycle bin?" >
					<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This article will be deleted permanently. Are you sure?</p>
				</div>
				<table class="tbl-data">
					<thead>
						<tr>
							<th>No.</th>
							<th class="tdlong">Title(VN)</th>
							<th class="tdlong">Title(EN)</th>
							<th class="tdlong">Posted Date</th>
							<th class="tdlong">Author</th>
							<th>Active</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
				<?php
					$postLst = DataProvider::executeQuery($sql);
					if($postLst != false){
						$idx = 1;
						while($row = $postLst->fetch_assoc()){ ?>
							<tr id="article<?php print $row["id"];?>">
								<td><?php print $idx; ?></td>
								<td class="tdlong"><a href="newpost.php?id=<?php print $row["id"]; ?>"><?php print $row["title_vn"]; ?></a></td>
								<td><?php print $row["title_en"]; ?></td>
								<td class="tdlong"><?php print $row["createdDate"]; ?></td>
								<td><?php print $row["username"]; ?></td>
								<td style="vertical-align:middle"><input type="checkbox" class="clschkactive" id="chk<?php print $row["id"];?>" name="actives[]" value="yes" <?php print ($row["active"]==1)?"checked":""; ?> /></td>
								<td style="vertical-align:middle"><a href="#" class="clsdelpost" id="<?php print $row["id"];?>"><img width="15" alt="Delete" height="23" src="../images/delete76x95.png"></img></a></td>
							</tr>
						<?php $idx++; }
					}
				?>
				</tbody>
				</table>
			</div>
			<div id="div_footer" >Copyright 2013 @Dao Anh Vu</div>
		</div>
	</body>
</html>
